﻿using System;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Windows;

namespace Bookvar.ObjectModel
{
    [DataContract]
    [Serializable]
    public class VisualSize : INotifyPropertyChanged, IEquatable<VisualSize>
    {
        private double height;
        private double width;

        public VisualSize()
        {
        }

        public VisualSize( double width, double height )
        {
            this.width = width;
            this.height = height;
        }

        public VisualSize( Size size )
        {
            width = size.Width;
            height = size.Height;
        }

        [DataMember]
        public double Width
        {
            get { return width; }
            set
            {
                if ( value != width )
                {
                    width = value;
                    OnPropertyChanged( new PropertyChangedEventArgs( "Width" ) );
                }
            }
        }

        [DataMember]
        public double Height
        {
            get { return height; }
            set
            {
                if ( value != height )
                {
                    height = value;
                    OnPropertyChanged( new PropertyChangedEventArgs( "Height" ) );
                }
            }
        }

        public bool IsEmpty
        {
            get { return width <= 0.0 || height <= 0.0; }
        }

        public override int GetHashCode()
        {
            return width.GetHashCode() ^ Height.GetHashCode();
        }

        /// <summary>
        /// Converts the visual size to the standard <see cref="System.Windows.Size"/>. 
        /// </summary>
        /// <returns>The size converted to windows size.</returns>
        public Size ToWindowsSize()
        {
            var size = new Size( Width, Height );
            return size;
        }

        #region Equals overrides
        
        public bool Equals( VisualSize other )
        {
            return Equals( this, other );
        }

        public override bool Equals( object obj )
        {
            var size = obj as VisualSize;
            if ( size == null )
            {
                return false;
            }
            return Equals( this, size );
        }

        public static bool Equals( VisualSize size1, VisualSize size2 )
        {
            if ( size1.Width.Equals( size2.Width ) )
            {
                return size1.Height.Equals( size2.Height );
            }
            return false;
        }

        #endregion

        #region PropertyChanged event

        [field : NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged( PropertyChangedEventArgs args )
        {
            if ( PropertyChanged != null )
            {
                PropertyChanged( this, args );
            }
        }

        #endregion
    }
}